package data;

import data.tileables.Resources;

/**************************************************************************************
 * Author(s): Changmin Bae, Eric Martin-Taylor
 * Holds a Resource object and comprises a StructLinkedList
 **************************************************************************************/
public class HarvestNode {
	int type, workers;
	Resources resource;
	
	private HarvestNode next, previous;
	private final int FOOD = 1,	METAL = 2, POWER = 3;
	private static int rate = 10;
	
	public HarvestNode(Resources temp, int _type){
		resource = temp;
		next = null;
		previous = null;
		type = _type;
		workers=0;
		resource.setTypeHarvested(_type);
	}
	
	
	// ========== GETTERS ============
	public int type(){return this.type;}
	public HarvestNode next(){return next;}
	public Resources getResource(){return resource;}
	public boolean hasNext(){ return (!(next==null));}
	public String toString(){return resource.toString()+ " Workers: "+workers;}
	public boolean equals(Resources resources){ return (resource.equals(resources));}
	
	
	
	
	// ========== SETTERS ============
	public void next(HarvestNode temp){next = temp;}
	public void previous(HarvestNode temp){previous = temp;}
	public void end(){previous.next(null);}
	
	public void die(){remove();}
	
	public int remove(){
		resource.unassignStructure();
		int x = workers;
		workers = 0;
		
		if(previous == null && next == null) return x;
		
		if(previous != null) 
			previous.next(next);
		if(next == null)
			previous.end();
		
		return x;
	}
	
	
	// ========== DO STUFF ===========
	
	// TODO - makeup a harvest method for resources
	public int harvest()
	{
		return resource.harvest(workers * rate);
	}
	
	
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * increment number of workers on resource
	 **************************************************************************************/
	public void assignWorkers(int x)
	{
		resource.assignWorkers(x);
		this.workers += x;
	}
	
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * decrement number of workers on resource and returns freed worker count
	 **************************************************************************************/
	public int unassignWorkers(int x)
	{
		this.workers -= x;
		return resource.unassignWorkers(x);
	}
	
}